﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Infragistics.WebUI.UltraWebGrid;

public partial class page_GroupCompetence_Manager : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.InitComponents();
            this.LoadGroup();               // 加载所有的组
            this.LoadCompetences();         // 加载所有的权限
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

//        RequestCompetences = Competences.BuildCompetencesList(Competences.Competence);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void tvOperGroup_SelectedNodeChanged(object sender, EventArgs e)
    {
        string ID = tvOperGroup.SelectedNode.Value.ToString();
        this.LoadGroupComp(ID);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string sCompetenceID = "";
        for (int i = 0; i < wgMain.Rows.Count; i++)
        {
            if (wgMain.Rows[i].Cells[1].Value.ToString() == "true" || wgMain.Rows[i].Cells[1].Value.ToString() == "1")//该行否已选
            {
                sCompetenceID = sCompetenceID + (wgMain.Rows[i].Cells[0].Value) + ",";
            }
        }

        string sGroupID = tvOperGroup.SelectedNode.Value.ToString();
        int GroupID = Shove._Convert.StrToInt(sGroupID, 0);

        int j = DAL.Procedures.P_SetGroupOfCompetences(GroupID, sCompetenceID, _Site.ID);

        if (j >= 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "更新成功！");
        }
    }

    private void InitComponents()
    {
        Utility.InitUltraWebGrid.InitDisplayLayOut_Oper(this.wgMain);
        this.wgMain.Height =300;
    }

    /// <summary>
    /// 加载所有的组
    /// </summary>
    private void LoadGroup()
    {
        DataTable dt = null;
        DAL.Tables.tbl_CustomerGroups table = new DAL.Tables.tbl_CustomerGroups();

        if (_Customer == null)
        {
            Response.Write(" <script> top.location.href='../Login.aspx'; </script> ");
            return;
        }

        long ShopID = _Customer.ShopID;

        dt = table.Open("", "", "");

        if (dt != null)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode node = new TreeNode();
                node.Text = dt.Rows[i]["Name"].ToString();
                node.Value = dt.Rows[i]["ID"].ToString();
                this.tvOperGroup.Nodes.AddAt(i, node);
            }
        }
    }

    /// <summary>
    /// 加载所有的权限
    /// </summary>
    private void LoadCompetences()
    {
        DAL.Tables.tbl_Competences table = new DAL.Tables.tbl_Competences();

        wgMain.DataSource = table.Open("", "", "");
        wgMain.DataBind();
    }

    /// <summary>
    /// 加载某组所拥有的权限信息
    /// </summary>
    private void LoadGroupComp(string ID)
    {
        DataTable dt = null;
        DAL.Tables.tbl_CompetencesOfGroups table = new DAL.Tables.tbl_CompetencesOfGroups();
        int groupID = Shove._Convert.StrToInt(ID, 0);

        dt = table.Open("[CompetenceID]", "[GroupID]=" + groupID, "");
        if (dt != null)
        {
            for (int i = 0; i < wgMain.Rows.Count; i++)
            {
                wgMain.Rows[i].Cells[1].Value = 0;      //将已前的状态清除
            }

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                string tempID = dt.Rows[j][0].ToString(); //查询出该组所有权限
                for (int i = 0; i < wgMain.Rows.Count; i++)
                {
                    string id = wgMain.Rows[i].Cells[0].Value.ToString();

                    if (tempID == id)
                    {
                        wgMain.Rows[i].Cells[1].Value = 1; //将状态置为选中
                    }
                }
            }
        }
    }

}

